package handler

import (
	"github.com/gin-gonic/gin"
	"recruit-ease/model"
	"strconv"
)

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	var count int64
	model.DB.Model(&model.Ease{}).Count(&count)
	var ease []model.Ease
	err := model.DB.Offset(offset).Limit(size).Find(&ease).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code":  200,
			"msg":   "查询成功",
			"data":  ease,
			"total": count,
		})
		return
	}
}

func Detail(c *gin.Context) {
	id := c.Query("id")
	var ease model.Ease
	err := model.DB.Where("id = ?", id).First(&ease).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "查询成功",
			"data": ease,
		})
		return
	}
}

func Coll(c *gin.Context) {
	id := c.Query("id")
	var ease model.Ease
	err := model.DB.Where("id = ?", id).First(&ease).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "收藏失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "收藏成功",
		})
		return
	}
}
